package com.homeWorkMapReduce;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import java.io.IOException;

/**
 * @description:
 * @projectName: hadoopstudy
 * @see: com.homeWorkMapReduce
 * @author: 崔宗祺
 * @createTime: 2024/5/9 20:39
 * @version: 1.0
 */
public class StudentLocationMapReduce extends Configured implements Tool {
    static class LocationMapper extends Mapper<LongWritable, Text, Text, Text> {
        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Text>.Context context) throws IOException, InterruptedException {
            // 获取当前数据的文件名
            String fileName = ((FileSplit) context.getInputSplit()).getPath().getName();
            // 如果是location_student.txt文件，则提取学号和地址信息
            if (fileName.equals("location_student.txt")) {
                String[] fields = value.toString().split("\t");
                if (fields.length == 4) {
                    String studentId = fields[0].trim();
                    String province = fields[1].trim();
                    String city = fields[2].trim();
                    context.write(new Text(studentId), new Text("L," + province + "," + city));
                }
            } else if (fileName.equals("student.txt")) {
                // 如果是student.txt文件，则提取学号和学生信息
                String[] fields = value.toString().split("\t");
                if (fields.length == 4) {
                    String studentId = fields[0].trim();
                    String name = fields[1].trim();
                    context.write(new Text(studentId), new Text("S," + name));
                }
            }
        }
    }

    static class LocationReducer extends Reducer<Text, Text, Text, Text> {
        @Override
        protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, Text>.Context context) throws IOException, InterruptedException {
            String studentId = key.toString();
            String province = "";
            String city = "";
            String name = "";

            for (Text value : values) {
                String[] fields = value.toString().split(",");
                if (fields.length == 3) {
                    if (fields[0].equals("L")) {
                        province = fields[1];
                        city = fields[2];
                    } else if (fields[0].equals("S")) {
                        name = fields[1];
                    }
                }
            }

            if (!province.isEmpty() && !city.isEmpty() && !name.isEmpty()) {
                context.write(new Text(studentId), new Text(name + "\t" + province + "\t" + city));
            }
        }
    }

    @Override
    public int run(String[] strings) throws Exception {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);
        job.setJobName("cuiozngqi");
        job.setJarByClass(StudentLocationMapReduce.class);
        job.setMapperClass(LocationMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

        job.setReducerClass(LocationReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);

        FileInputFormat.addInputPath(job, new Path("/user/briup/student"));
        FileInputFormat.addInputPath(job, new Path("/user/briup/location_student"));
        FileOutputFormat.setOutputPath(job, new Path("/user/briup/output"));
        return job.waitForCompletion(true) ? 0 : 1;
    }

    public static void main(String[] args) throws Exception {
        System.exit(ToolRunner.run(new StudentLocationMapReduce(), args));
    }
}
